﻿// ---------------------------------------------------------------------
// Lớp BaseEffect
//
// Nội dung:
//    + Định nghĩa lớp cơ bản quản lý 1 loại script
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagementEffect.Engine;
using Common;
using ServerYuGi;

namespace ManagementEffect
{
    public class Base_CheckPoint 
    {
        public struct Item_Effect
        {
            public int ID { get; set; }
            public string ScriptCheck { get; set; }
            public string ScriptExe { get; set; }
            public TypeCheck typeCheck { get; set; }
            public bool isAsk { get; set; }
        }
        protected List<Item_Effect> danhsach = new List<Item_Effect>();
        public List<Item_Effect> DanhSach
        {
            get
            {
                return danhsach;
            }
        }
        protected List<Item_Effect> danhsach_noask = new List<Item_Effect>();
        public List<Item_Effect> DanhSachNoAsk
        {
            get
            {
                return danhsach_noask;
            }

        }
        internal Queue<Item_Effect> XoaSauActive = new Queue<Item_Effect>();
        #region IManagementEffect Members

        public virtual void Active(PythonEngine engine)
        {
            foreach (var item in danhsach)
            {
                try
                {
                    engine.SetCode(item.ScriptExe);
                    engine.Excute();
                }catch(Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
        }
        public virtual void ActiveWithID(int ID,PythonEngine engine)
        {
            foreach (var item in danhsach)
            {
                try
                {
                    if (item.ID == ID)
                    {
                        engine.SetCode(item.ScriptExe);
                        engine.Excute();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
        }
        public static void ActiveWithScript(string ScriptExcute, PythonEngine engine)
        {
            try
            {
                engine.SetCode(ScriptExcute);
                engine.Excute();
                return;

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Script :" + ex.Message);
                return;
            }

        }
        /// <summary>
        /// Truong Hop Active co hoi nguoi choi
        /// </summary>
        /// <param name="engine"></param>
        /// <param name="super"></param>
        public virtual void Check(PythonEngine engine,SSupervisor super)
        {
            foreach (var item in danhsach)
            {
                try
                {
                    if (!CheckTypeReCheck(item, super))
                        continue;
                    engine.SetCode(item.ScriptCheck);
                    engine.Excute();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
            
            // 15/09/2010, Hau fix, Khong xoa het script cua la bai bi xoa o cac checkpoint
            foreach (Base_CheckPoint value in super.EffectTurnManager.Factory.Values)
            {
                while (value.XoaSauActive.Count != 0)
                {
                    Item_Effect temp = value.XoaSauActive.Dequeue();
                    value.danhsach.Remove(temp);
                    value.danhsach_noask.Remove(temp);
                    Console.WriteLine("Remove Script ID={0} ", temp.ID);
                }
            }
        }

        /// <summary>
        /// Truong Hop Active Khong Can Hoi
        /// </summary>
        /// <param name="engine"></param>
        /// <param name="super"></param>
        public virtual void CheckNoAsk(PythonEngine engine, SSupervisor super)
        {
            foreach (var item in danhsach_noask)
            {
                try
                {
                    if (!CheckTypeReCheck(item, super))
                        continue;
                    engine.SetCode(item.ScriptCheck);
                    engine.Excute();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
            while (XoaSauActive.Count != 0)
            {
                Item_Effect temp = XoaSauActive.Dequeue();
                danhsach_noask.Remove(temp);
                danhsach.Remove(temp);
                Console.WriteLine("Remove Script ID={0} ", temp.ID);
            }
        }

        bool CheckTypeReCheck(Item_Effect item,SSupervisor super)
        {
            SPlayer playerOwner = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            if (playerOwner.CheckIDInAll(item.ID))
            {
                if (item.typeCheck == TypeCheck.Both || item.typeCheck == TypeCheck.MyTurn)
                    return true;
                else
                    return false;
            }
            playerOwner = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            if (playerOwner.CheckIDInAll(item.ID))
            {
                if (item.typeCheck == TypeCheck.Both || item.typeCheck == TypeCheck.OtherTurn)
                    return true;
                else
                    return false;
            }
            return false;
        }

        public string GetScriptExecute(int ID)
        {
            foreach (var item in danhsach)
            {
                try
                {
                    if (item.ID == ID)
                        return item.ScriptExe;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
            foreach (var item in danhsach_noask)
            {
                try
                {
                    if (item.ID == ID)
                        return item.ScriptExe;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Script :" + ex.Message);
                    continue;
                }
            }
            return "";
        }

        #endregion
    }
}
